/**
 * 头部页面JS
 * 
 * @author li.sanlai@ustcinfo.com
 */

Header = function() {
	
//	var loading = '<img src="/resources/img/loading.gif"/>&nbsp;loading……';

	return {

		//初始化顶部菜单
		initTopMenu : function() {
			$.ajax({
				url : "pc/menu/topmenu.htm",
				type:'POST',
				dataType:'JSON',
				context : document.body,
				success : function(data) {
					var link = '<ul>';
					for ( var i = 0; i < data.length; i++) {
						var m = data[i];
						link += Header.createTopLink(m,i);
					}
					link += '</ul>';
					$('#topMenu').html(link);
					$(".topMenuClass[title='首页']").addClass('curTopMenu');
				}
			});
		},
		
		//生成顶部菜单按钮的连接
		createTopLink:function(m,i){
			var link = '<li>' 
						+'<a title="'+ m.menuName +'" class="topMenuClass" onclick="Header.changeTopMenuStyle(this.id);" id="'+m.menuId+'" href="javascript:Header.loadSubMenu(\''+m.menuId+'\',\''+m.menuName+'\');">'
						+ '<img src="/resources/img/icon/'+m.icon+'" />'+m.menuName
						+'</a>'
						+'</li>';
			return link;
		},
		
		//改变顶部菜单选中样式
		changeTopMenuStyle:function(id){
			$('.topMenuClass').removeClass('curTopMenu');
			$('#'+id).addClass('curTopMenu');
		},
		
		//重载tab
		reloadTab : function(){
			var tab = $('#mainPanel').tabs('getSelected');  // get selected panel
			var title = tab.panel('options').title;
			if(title=='首页'){
				$('#frame'+title).attr('src','/home.htm');
			}
		},
		
		//加载父菜单对应的子菜单
		loadSubMenu:function(menuId,menuName){
			if(menuName=="首页"){
				Header.subMenuClick(menuId,'/home.htm','首页');
			}else{
				$('body').layout('add',{
			    	id:'menuRegion',
			    	iconCls:'list',
			        region: 'west',  
			        width: 220,  
			        title: menuName,  
			        split: true,
			        content:'<div id="leftMenu" class="leftMenu"></div>',
			        onExpand:function(){
			        	Header.reloadTab();
			        },
			        onCollapse:function(){
			        	Header.reloadTab();
//			        },
//			        onResize:function(width, height){
//			        	Header.reloadTab();
			        }
			    });  
				
				$('#menuRegion').panel('open');
				$('#menuRegion').panel('setTitle',menuName);
				$('#menuRegion').mask("加载中……");
				$.ajax({
					url : "pc/menu/submenu/"+menuId+'.htm',
					type:'POST',
					dataType:'JSON',
					context : document.body,
					success : function(data) {
						var link = '<ul>';
						for ( var i = 0; i < data.length; i++) {
							var m = data[i];
							link += Header.createSubMenuLink(m);
						}
						
						link += '</ul>';
						$('.leftMenu').html(link);
						Header.openRigth(data[0]);
						$('#menuRegion').unmask();
					},
					statusCode: {401: function() {
						alert(401);
						//如果是顶层窗口
						if (window.top !== window.self) {
							window.parent.location.href = "/security/sessionTimeoutHandler";
						} else {
							window.location.href = "/security/sessionTimeoutHandler";
						}
					}},
					error:function(jqXHR, textStatus, errorThrown){
						$('.leftMenu').html("菜单加载出错");
						$('#menuRegion').unmask();
					}
				});
			}
		},
		
		//默认打开右边首页
		openRigth:function(data){
			$('#'+data.menuId).addClass('selected');
			Ict.addTab(data.menuName,data.path,null,true);
		},
		
		//创建左边菜单的连接
		createSubMenuLink:function(m){
			var link = '<li>' 
				+'<a class="subMenuClass" onClick="Header.changeSubMenuStyle(this.id);" id="'+m.menuId+'" href="javascript:Header.subMenuClick(\''+m.menuId+'\',\''+m.path+'\',\''+m.menuName+'\');">'
				+ m.menuName
				+'</a></li>';
			return link;
		},
		
		//改变子菜单的选中样式
		changeSubMenuStyle : function(id){
			$('.subMenuClass').removeClass('selected');
			$('#'+id).addClass('selected');
		},
		
		//子菜单被单击
		subMenuClick : function(menuId,href,title){
			//先判断session是否过期
			$.ajax({
				url : "/pc/menu/filter.htm",
				type:'POST',
				data:{
					'menuId'	: menuId,
					'url' 		: href
				},
				dataType:'JSON',
				context : document.body,
				success : function(data) {
					Ict.addTab(title,href,null,true);
				}
			});
		},
		
		//注销
		logout : function(){
			Ict.confirm('确定要注销吗？',function(b){
				if(b){
					window.location.href = "/logout";
				}
			});
		},
		
		//打开修改密码窗口
		changePwd :function(){
			Ict.openWin("修改密码", 350, 230, "/pc/systemUser/password.htm");
		}
	};

}();

$(document).ready(function() {
	Header.initTopMenu();
});